module.exports = Behavior({
    methods: {
        //打开链接
        hqOpen(e) {
            let url;
            if (typeof e === 'string') {
                url = e;
            } else {
                url = e.currentTarget.dataset.url;
            }
            if (!url) return;
            if (!url.startsWith('http')) {
                // 小程序内
                if (!url.startsWith('/')) {
                    url = '/' + url;
                }
                wx.navigateTo({
                    url: url,
                    fail() {
                        // 失败则尝试switchTab
                        let tabData = url.split('?');
                        wx.setStorageSync('hqOpen', tabData);
                        wx.switchTab({
                            url: tabData[0],
                        });
                    }
                });
            } else { // webview
                url = encodeURIComponent(url);
                wx.navigateTo({ url: '/hq-app/web/web?url=' + url });
            }
        },
        //打电话组件
        hqCall: function (e) {
            wx.makePhoneCall({
                phoneNumber: e.currentTarget.dataset.mobile, //此号码并非真实电话号码，仅用于测试
                success: function () {
                    //console.log("拨打电话成功！")
                },
                fail: function () {
                    //console.log("拨打电话失败！")
                }
            })
        },
        
        initLoad() {
            console.log('initLoad');
        },

        onLoad(options) {
            this.setData(options);

            this.initLoad();
        },

        onPullDownRefresh() {
            this.initLoad();
            wx.stopPullDownRefresh();
        },

        onShareAppMessage() {
            return {};
        },
    }
});